﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JYH.Framework
{
    public class RadixConverterHelper
    {


        /// <summary>
        /// 十进制转十六进制
        /// </summary>
        /// <param name="ten"></param>
        /// <returns></returns>
        public static string Ten2Hex(string ten)
        {
            ulong tenValue = Convert.ToUInt64(ten);
            ulong divValue, resValue;
            string hex = "";
            do
            {
                //divValue = (ulong)Math.Floor(tenValue / 16);

                divValue = (ulong)Math.Floor((decimal)(tenValue / 16));

                resValue = tenValue % 16;
                hex = TenValue2Char(resValue) + hex;
                tenValue = divValue;
            }
            while (tenValue >= 16);
            if (tenValue != 0)
                hex = TenValue2Char(tenValue) + hex;
            return hex;
        }

        /// <summary>
        /// 十进制数字转十六进制字符
        /// </summary>
        /// <param name="ten"></param>
        /// <returns></returns>
        public static string TenValue2Char(ulong ten)
        {
            switch (ten)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    return ten.ToString();
                case 10:
                    return "A";
                case 11:
                    return "B";
                case 12:
                    return "C";
                case 13:
                    return "D";
                case 14:
                    return "E";
                case 15:
                    return "F";
                default:
                    return "";
            }
        }


        /// <summary>
        /// 十六进制字符串转Byte数组
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static byte[] ConvertHexStrToByteArray(string hexStr)
        {
            hexStr = hexStr.Replace(" ", "").Replace("\n", "");
            string[] array = new string[hexStr.Length / 2];
            byte[] bytes = new byte[array.Length];
            for (int i = 0; i < hexStr.Length; i++)
            {
                array[i / 2] += hexStr[i];
            }
            for (int i = 0; i < array.Length; i++)
            {
                bytes[i] = Convert.ToByte(array[i], 16);
            }
            return bytes;
        }



        private static byte[] HexStringToByteArray(string hexString)
        {
            hexString = hexString.Replace(" ", "").Replace("-", "").ToUpper().Trim();
            if (hexString.Length % 2 != 0)
            {
                hexString += " ";
            }
            int bytesCount = hexString.Length / 2;
            byte[] bytes = new byte[bytesCount];

            for (int i = 0; i < bytesCount; i++)
            {
                string byteString = hexString.Substring(i * 2, 2);
                bytes[i] = Convert.ToByte(byteString, 16);
            }

            return bytes;
        }

        public static uint CalcCRC16(string hexCommand)
        {
            byte[] pBuf = HexStringToByteArray(hexCommand);
            uint i, j;
            uint crc16 = 0xFFFF;
            for (i = 0; i < pBuf.Length; i++)
            {
                crc16 ^= pBuf[i];
                for (j = 0; j < 8; j++)
                {
                    if ((crc16 & 0x0001) != 0)
                    {
                        crc16 >>= 1; // 右移一位
                        crc16 ^= 0xA001; // 异或多项式0xA001
                    }
                    else
                    {
                        crc16 >>= 1; // 右移一位
                    }
                }
            }
            return crc16;
        }


        public static string Hex2Ten(string hex)
        {
            int ten = 0;
            for (int i = 0, j = hex.Length - 1; i < hex.Length; i++)
            {
                ten += HexChar2Value(hex.Substring(i, 1)) * ((int)Math.Pow(16, j));
                j--;
            }
            return ten.ToString();
        }

        /// <summary>
        /// 16进制字符转10进制数值
        /// </summary>
        /// <param name="hexChar"></param>
        /// <returns></returns>
        private static int HexChar2Value(string hexChar)
        {
            switch (hexChar)
            {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    return Convert.ToInt32(hexChar);
                case "a":
                case "A":
                    return 10;
                case "b":
                case "B":
                    return 11;
                case "c":
                case "C":
                    return 12;
                case "d":
                case "D":
                    return 13;
                case "e":
                case "E":
                    return 14;
                case "f":
                case "F":
                    return 15;
                default:
                    return 0;
            }
        }
    }
}
